package com.ltf.Utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

//通用
public class PackJson {
    //控制层每个方法，统一使用的返回数据打包格式，由@ResponseBody 转化JSON
    public  static XJson packX(int code,String msg,Object data){
        return new XJson(code,msg,data);
    }
    //转json
    static Logger logger=LoggerFactory.getLogger(PackJson.class);
    static ObjectMapper objectMapper=new ObjectMapper();
    public static String packJson(int code,String msg,Object data){
        Map<String,Object> m=new HashMap<>();
        m.put("code",code);
        m.put("msg",msg);
        m.put("data",data);
        //把 m 转JSON字符串
        String json="";
        try {
            json= objectMapper.writeValueAsString(m);
        }catch (Exception e){
            //error 是保证会输出
            logger.error(e.getMessage());
        }
        logger.debug("转成了JSON="+json);
        return json;
    }

    public static Map<String, Object> packM(int code,String msg,Object data){
        HashMap<String,Object> map=new HashMap<>();
        map.put("code",code);
        map.put("msg",msg);
        map.put("data",data);
        return map;
    }
}
